﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using Vwlab;

using XPTable;
using XPTable.Editors;
using XPTable.Events;
using XPTable.Models;
using XPTable.Renderers;
using XPTable.Sorting;


namespace WinBird
{
    public partial class MainForm : Form
    {
        public static string[] AppVersionFull = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString().Split(',', '.');
        public static string AppVersion = AppVersionFull[0] + "." + AppVersionFull[1] + "." + AppVersionFull[2] + ((AppVersionFull[3] != "0") ? "." + AppVersionFull[3] : "");
        public static string AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
        public static string AppTitle = AppName + " v" + AppVersion;

        Api api = new Api();
        Error err = new Error();
        uBass uBass = new uBass();
        Update update = new Update();


        TextColumn friends_viewColumn;
        TextColumn friends_titleColumn;
        TextColumn friends_idColumn;

        NumberColumn audio_numColumn;
        TextColumn audio_aidColumn;
        TextColumn audio_playingColumn;
        CheckBoxColumn audio_titleColumn;
        TextColumn audio_durationColumn;
        ButtonColumn audio_buttonColumn;
        ProgressBarColumn audio_progressColumn;

        Color[] theme = new Color[] { 
            Styles.HexToColor("#162029"), // background
            Styles.HexToColor("#19232C"), //line 1
            Styles.HexToColor("#131D26"), //line 2
            Styles.HexToColor("#344754"), //selected
            Styles.HexToColor("#344754"), //sorted column
        };

        public MainForm()
        {
            InitializeComponent();

            this.notifyIcon.Text = this.Text = AppTitle;

            //this.listFriends.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] { groupMessages, groupOnline, groupOffline });

            #region listFriends
            friends_idColumn = new TextColumn("", 0);
            friends_idColumn.Editable = false;
            friends_idColumn.Visible = false;
            friends_idColumn.Enabled = false;

            friends_viewColumn = new TextColumn("", 30);
            friends_viewColumn.Editable = false;
            friends_viewColumn.Sortable = false;

            friends_titleColumn = new TextColumn("Пользователи", 200);
            friends_titleColumn.Editable = false;
            friends_titleColumn.Sortable = false;

            this.listFriends.ColumnModel = new ColumnModel(new Column[] {
                friends_viewColumn,
                friends_titleColumn,
                friends_idColumn
            });

            this.listFriends.TableModel = new TableModel();
            this.listFriends.TableModel.RowHeight = 30;
            this.listFriends.ContextMenuStrip = this.menuListFriends;

            #endregion

            #region listAudio
            audio_numColumn = new NumberColumn("#", 50);
            audio_numColumn.Editable = false;
            audio_numColumn.Alignment = ColumnAlignment.Right;

            audio_aidColumn = new TextColumn("Id", 100);
            audio_aidColumn.Editable = false;
            audio_aidColumn.Visible = false;

            audio_playingColumn = new TextColumn("Playing", 70);
            audio_playingColumn.Editable = false;
            audio_playingColumn.Alignment = ColumnAlignment.Center;

            audio_titleColumn = new CheckBoxColumn("Название", 400);

            audio_durationColumn = new TextColumn("Длительность", 105);
            audio_durationColumn.Editable = false;
            audio_durationColumn.Alignment = ColumnAlignment.Right;

            audio_buttonColumn = new ButtonColumn("", 30);

            audio_progressColumn = new ProgressBarColumn("", 60);
            audio_progressColumn.Visible = false;


            this.listAudio.ColumnModel = new ColumnModel(new Column[] {
                audio_numColumn,
                audio_aidColumn,
                audio_playingColumn,
                audio_titleColumn,
                audio_durationColumn,
                audio_buttonColumn,
                audio_progressColumn
            });

            this.listAudio.TableModel = new TableModel();

            this.listAudio.GridColor = System.Drawing.Color.Silver;

            this.listAudio.TableModel.RowHeight = 25;
            #endregion
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            uBass.Init();
            this.Login();
        }

        private void Init()
        {
            try
            {
                Array[] result = api.getApi("getProfiles", "user",
                        new string[] { "uid", "first_name", "last_name", "photo_medium" },
                        new string[,] { { "uid", api.UserID.ToString() } }
                        );

                foreach (string[] content in result)
                {
                    this.notifyIcon.Text = this.Text = content[1] + " " + content[2] + " - " + AppTitle;
                }
            }
            catch (Exception e)
            {
                err.setError(e.Message);
            }

            this.Friends();
            this.Audio();

            this.getAlbums();

            this.panelMain.Visible = true;

            /*
            if (update.getUpdate(AppVersion) == true)
            {
                var result = err.setQuestion("Вышла новая версия программы, обновить?");

                if (result == DialogResult.Yes)
                {
                    update.download();
                }
            }*/
            
        }

        public void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                threadOnlineFriends.Abort();
            }
            catch { }

            Application.Exit();
        }

        private void buttonMyAudio_Click(object sender, EventArgs e)
        {
            foreach (XPTable.Models.Row r in this.listFriends.TableModel.Rows)
            {
                r.Cells[0].Text = "";
            }

            api.UserID = api.homeUserID;
            this.getAlbums(api.homeUserID);
            this.getAudio(api.homeUserID);
        }

        private void buttonLogout_Click(object sender, EventArgs e)
        {
            var result = err.setQuestion("Вы точно хотите выйти из профиля?");
            if (result == DialogResult.Yes)
            {
                this.buttonStop_Click(null, null);
                this.Logout();
            }
        }

        private void menuViewMyFriends_Click(object sender, EventArgs e)
        {
            this.getFriends();
        }

        private void buttonMyFriends_Click(object sender, EventArgs e)
        {
            this.getFriends();
        }

        private void boxSearch_Enter(object sender, EventArgs e)
        {
            if (this.boxSearch.Text == "Поиск музыки")
            {
                this.boxSearch.Text = "";
                this.boxSearch.ForeColor = Color.Black;
            }
        }

        private void boxSearch_Leave(object sender, EventArgs e)
        {
            if (this.boxSearch.Text == "")
            {
                this.boxSearch.Text = "Поиск музыки";
                this.boxSearch.ForeColor = Color.Gray;
            }
        }

        #region Minimize form
        FormWindowState lastForm;

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized) { this.ShowInTaskbar = false; }
            else { lastForm = this.WindowState; }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left && this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = lastForm;
                this.ShowInTaskbar = true;
            }
        }
        #endregion

    }
}
